package cn.itcast.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
public class SignController {
    String paypass="891223";
    String token="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImp0aSI6Ik1JTkdZQU5HQjdEMEM5RDI3OTg5Mjg1N0RFNTQ5MEFCODU2NzcyRkUifQ.eyJpc3MiOiJodHRwczpcL1wvd3d3Lm15enkuY29tLmNuIiwiYXVkIjoiaHR0cHM6XC9cL3d3dy5teXp5LmNvbS5jbiIsImp0aSI6Ik1JTkdZQU5HQjdEMEM5RDI3OTg5Mjg1N0RFNTQ5MEFCODU2NzcyRkUiLCJpYXQiOjE2NjM2NDY0NDYsIm5iZiI6MTY2MzY0NjQ0NiwiZXhwIjoxNjYzNjUwMDQ2LCJ1dWlkIjoxMjY4OSwiY2xpZW50X3R5cGUiOjF9.NmPBDSGtm9R0ZgfgK5KaRzZrvaYs_n0AxfX3DyJ81ac";
    String createOrderUrl="https://m.yocho.art/v1/createOrder";
    private static List<IpBean> list=new ArrayList<>();
    @RequestMapping("/createOrder")
    public String createOrder(@RequestParam(value = "num",defaultValue = "1000") Integer num) {
        Long currentTime=new Date().getTime();
        String temp="buy_passwd=891223&domain=https://m.yocho.art/&goods_num=1&id=8&key=MINGYANGB7D0C9D279892857DE5490AB856772FE&ordertype=0&pathinfo=v1/createOrder&paytype=balance&special_id=8&timestamp="+currentTime;
        String sign=SecureUtil.md5(temp);
        JSONObject requestJson = new JSONObject();
        requestJson.append("buy_passwd", "891223");
        requestJson.append("goods_num", "1");
        requestJson.append("id", "8");
        requestJson.append("ordertype", "0");
        requestJson.append("paytype", "balance");
        requestJson.append("special_id", "8");
        String result =null;
        try{
            String requestParam = requestJson.toString();
            result = HttpRequest.post(createOrderUrl).body(requestParam)
                    .header("token",token)
                    .header("timestamp",currentTime+"").
                    header("sign",sign).execute().body();
            //result="{\"code\":1,\"data\":{\"order_id\":\"123\"}}";
            System.out.println("#####创建订单返回参数####"+result+"#######"+System.currentTimeMillis());
            if(StrUtil.isNotBlank(result)){
                JSONObject str = JSONUtil.parseObj(result);
                String code=str.get("code").toString();
                if(code.equals("200")){
                    System.out.println("#####创建订单成功####"+result+"#######"+System.currentTimeMillis());
                    String data=str.get("data").toString();
                    JSONObject orderIdJson=JSONUtil.parseObj(data);
                    String orderId=orderIdJson.get("order_id").toString();
                }
            }

        }catch (Exception e){
            System.out.println("#####创建订单成功####"+result);
        }
        return null;
    }

}
